package com.hsy.crm.web.web;

import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.Response;
import com.hsy.crm.web.domain.cm.Unified;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class BaseAction{

    Logger logger = LoggerFactory.getLogger(BaseAction.class);

    @ExceptionHandler(Exception.class)
    public void processMethod(Exception e, HttpServletRequest request , HttpServletResponse response) throws IOException {
        Unified<String> unified = Unified.create(String.class);
        unified.setResult(App.Result.UNSAFE_ERROR);
        unified.setData(e.getMessage());
        response.getWriter().print(Response.body(unified));
        logger.error("content",e);
    }

    protected static int getIntSession(HttpSession session, String key){
        Object obj = session.getAttribute(key);
        if( obj == null ){
            return 0;
        }else{
            return Integer.parseInt(obj.toString());
        }
    }

}
